<?php
  include "../../mainfile.php";
  $module_name = basename( dirname( __FILE__ )) ;
  include_once "include/xoops.php";
  include_once "seo_plugin.php";
  include_once "include/constants.php";  
  include XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
  $seo = (!empty($xoopsModuleConfig[$module_name.'_seourl']) && $xoopsModuleConfig[$module_name.'_seourl']>0) ? intval($xoopsModuleConfig[$module_name.'_seourl']) : 0;
  include_once "include/info.class.php";
  $op = (isset($_POST['op'])) ? $_POST['op'] : ((isset($_GET['op'])) ? $_GET['op'] : "show");
  if ($op=="save" && !isset($_POST['post'])) $op="editit";
  $id = (isset($_POST['id'])) ? intval($_POST['id']) : ((isset($_GET['id'])) ? intval($_GET['id']) : 0); // FIX for editing
  $cat = (isset($_POST['homepage'])) ? intval($_POST['homepage']) : ((isset($_GET['homepage'])) ? intval($_GET['homepage']) : 1);
  $return = (isset($_POST['return'])) ? intval($_POST['return']) : ((isset($_GET['return'])) ? intval($_GET['return']) : 0);
  $info = new infosite();
  $mod_isAdmin = ($xoopsUser && $xoopsUser->isAdmin()) ? true : false;
  if ($op=="save") {
	  if (!$GLOBALS['xoopsSecurity']->check()) {
        redirect_header("index.php",3,implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
        exit();
      }
	  if (is_readable(XOOPS_ROOT_PATH.'/Frameworks/captcha/captcha.php')) {
	    include_once XOOPS_ROOT_PATH.'/Frameworks/captcha/captcha.php';
		  $security = new XoopsCaptcha();
		  if (!$security->verify(true)) {
			  include_once XOOPS_ROOT_PATH.'/header.php';
			  if (intval($xoopsModuleConfig[$xoopsModule->getVar('dirname').'_showrblock']) == 1)
		      $GLOBALS['xoopsTpl']->assign( 'xoops_showrblock', 0 );
	          xoops_load_lang_file( 'admin', $xoopsModule->getVar('dirname'));
			  if ($id > 0 && $op!="editit") $info->load($id);
			  $info->setErrors(_INFO_SECURITY_ERROR);
              echo $info->getHtmlErrors();
	          $info->getform();
		      include_once XOOPS_ROOT_PATH.'/footer.php';
			  exit();
		  }
	    }
		$approve=0;
		$infothisgroups = (is_object($xoopsUser)) ? $xoopsUser->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
		$infoperm_handler = xoops_gethandler('groupperm');
		$show_info_perm = $infoperm_handler->getItemIds('InfoPerm', $infothisgroups, $xoopsModule->getVar('mid'));
		if (in_array(_CON_INFO_CANFREEALL,$show_info_perm) || $mod_isAdmin) $approve=1;
		if ($xoopsUser && $xoopsUser->uid()==$info->getVar('owner')) { // eigene Seite
		  if (in_array(_CON_INFO_CANUPDATE_SITEFULL,$show_info_perm)) $approve=1;
			if ($info->is_new() && in_array(_CON_INFO_ALLCANUPDATE_SITEFULL,$show_info_perm)) $approve=1;
		}
		if ($approve==1) {		  
	      if (!$info->insert()) {
		    include_once XOOPS_ROOT_PATH.'/header.php';
			if (intval($xoopsModuleConfig[$xoopsModule->getVar('dirname').'_showrblock']) == 1)
		    $GLOBALS['xoopsTpl']->assign( 'xoops_showrblock', 0 );
	        xoops_load_lang_file( 'admin', $xoopsModule->getVar('dirname'));
			if ($id > 0 && $op!="editit") $info->load($id);
            echo $info->getHtmlErrors();
	        $info->getform();
		    include_once XOOPS_ROOT_PATH.'/footer.php';
			exit();
		  } else {
		    xoops_load_lang_file( 'admin', $xoopsModule->getVar('dirname'));
			$mode=array("seo"=>$seo,"id"=>$info->getVar("storyid"),"title"=>$info->getVar("title"),"dir"=>$xoopsModule->dirname(),"cat"=>$info->getVar("homepage"));
	        eval ('$rurl = seo_plugin_'.$xoopsModule->dirname().'_make($mode);');	
			if ($return) 
		       redirect_header($rurl, 3, _INFO_DBUPDATED);
			else
			   redirect_header($rurl, 3, _INFO_DBUPDATED);
	         exit();
		  }
		} else {
		  if (!$info->backup()) {
		    include_once XOOPS_ROOT_PATH.'/header.php';
			if (intval($xoopsModuleConfig[$xoopsModule->getVar('dirname').'_showrblock']) == 1)
		    $GLOBALS['xoopsTpl']->assign( 'xoops_showrblock', 0 );
	        xoops_load_lang_file( 'admin', $xoopsModule->getVar('dirname'));
			if ($id > 0 && $op!="editit") $info->load($id);
            echo $info->getHtmlErrors();
	        $info->getform();
		    include_once XOOPS_ROOT_PATH.'/footer.php';
			exit;
		  } else {
		    xoops_load_lang_file( 'admin', $xoopsModule->getVar('dirname'));
			$seo = (!empty($xoopsModuleConfig[$xoopsModule->dirname().'_seourl'])) ? intval($xoopsModuleConfig[$xoopsModule->dirname().'_seourl']) : 0;
		    $mode=array("seo"=>$seo,"id"=>$info->getVar("storyid"),"title"=>$info->getVar("title"),"dir"=>$xoopsModule->dirname(),"cat"=>$info->getVar("homepage"));
	        eval ('$rurl = seo_plugin_'.$xoopsModule->dirname().'_make($mode);');	
			if ($return) 
		        redirect_header($rurl, 3, _INFO_DBUPDATED);
			  else
			    redirect_header($rurl, 3, _INFO_DBUPDATED);
	          exit();
			}
		}
  } else {
      include_once XOOPS_ROOT_PATH.'/header.php';
	  if (intval($xoopsModuleConfig[$xoopsModule->getVar('dirname').'_showrblock']) == 1) {
		  $GLOBALS['xoopsTpl']->assign( 'xoops_showrblock', 0 );
	  }
	  xoops_load_lang_file( 'admin', $xoopsModule->getVar('dirname'));
	  xoops_load_lang_file( 'modinfo', $xoopsModule->getVar('dirname'));
	  $id = (isset($_POST['id'])) ? intval($_POST['id']) : ((isset($_GET['id'])) ? intval($_GET['id']) : 0); // FIX for editing
	  if ($id > 0 && $op!="editit") $info->load($id);
	  $info->getform();	
	  include_once XOOPS_ROOT_PATH.'/footer.php';
 }
?>